package airport.server.DAO;

import airport.server.pojo.Hotel;
import airport.server.pojo.Panneau;
import airport.server.pojo.Vol;
import airport.shared.beans.HotelBean;
import airport.shared.beans.PanneauBean;
import airport.shared.beans.VolBean;
import com.google.gwt.i18n.shared.DateTimeFormat;
import com.google.gwt.i18n.shared.DefaultDateTimeFormatInfo;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Romain on 13/02/15.
 */
public class PanneauDAO extends DAO<PanneauBean> {

	public ArrayList<PanneauBean> selectAll() {
		List<Panneau> panneauList;
        ArrayList<PanneauBean> panneauBeanList = new ArrayList<>();
        PanneauBean panneau;

		connection();
        panneauList = session.createQuery("from Panneau order by dateaff desc,heureaff desc").list();


        Iterator i = panneauList.iterator();
        while (i.hasNext()) {
            panneau = pojoToBean((Panneau) i.next());
            panneauBeanList.add(panneau);
        }

        commitAndClose();

        return panneauBeanList;
    }

	public PanneauBean selectAllByDate() {
		List panneauList;
        PanneauBean panneauBean;
        Panneau panneau;

		Date dateActuelle = new Date();
		DateTimeFormat dtf = new DateTimeFormat("yyyy-MM-dd", new DefaultDateTimeFormatInfo()) {
		};
		DateTimeFormat dtg = new DateTimeFormat("HH:mm:ss", new DefaultDateTimeFormatInfo()) {
		};

		String laDate = dtf.format(dateActuelle);
		String lHeure = dtg.format(dateActuelle);
		connection();
		panneauList = session.createQuery("from Panneau p where (p.dateaff< '" + laDate + "') " +
				"OR (p.dateaff= '" + laDate + "' AND  p.heureaff<= '" + lHeure + "') " +
				"order by p.dateaff, p.heureaff").list();

        if (panneauList != null && panneauList.size() != 0) {
            panneau = (Panneau) panneauList.get(panneauList.size() - 1);
            panneauBean = pojoToBean(panneau);
        } else {
            panneauBean = null;
        }
        commitAndClose();

        return panneauBean;
    }

	@Override
	public String save(PanneauBean p) {
		connection();
        Panneau panneau = beanToPojo(p);
        session.save(panneau);
        commitAndClose();

		return "Panneau enregistré";
	}

	@Override
	public String update(PanneauBean p) {
		connection();
		session.update(beanToPojo(p));
		commitAndClose();

		return "Panneau mis à jour";
	}

	@Override
	public String delete(PanneauBean p) {
		connection();
		session.delete(beanToPojo(p));
		commitAndClose();

		return "Panneau supprimé";
	}

    private PanneauBean pojoToBean(Panneau panneau) {
        PanneauBean panneauBean = new PanneauBean();
        panneauBean.setId(panneau.getIdpanneau());
        panneauBean.setHotel(pojoToBeanHotel(panneau.getHotel()));
        panneauBean.setVol(pojoToBeanVol(panneau.getVol()));
        panneauBean.setDateaff(panneau.getDateaff());
        panneauBean.setHeureaff(panneau.getHeureaff());
        panneauBean.setImage(panneau.getImage());

        return panneauBean;
    }

    private Panneau beanToPojo(PanneauBean panneauBean) {
        Panneau panneau = new Panneau();
        panneau.setIdpanneau(panneauBean.getId());
        panneau.setHotel(beanToPojoHotel(panneauBean.getHotel()));
        panneau.setVol(beanToPojoVol(panneauBean.getVol()));
        panneau.setDateaff(panneauBean.getDateaff());
        panneau.setHeureaff(panneauBean.getHeureaff());
        panneau.setImage(panneauBean.getImage());

        return panneau;
    }

    //VOL

    private VolBean pojoToBeanVol(Vol vol) {
        HotelBean hotelBean;
        VolBean volBean = new VolBean();
        volBean.setId(vol.getIduser());
        volBean.setDate_A(vol.getDate_A());
        volBean.setDate_D(vol.getDate_D());
        volBean.setLieu_A(vol.getLieu_A());
        volBean.setLieu_D(vol.getLieu_D());
        volBean.setTarif(vol.getTarif());

        // ITERATOR
        Iterator i = vol.getHotels().iterator();
        while (i.hasNext()) {
            hotelBean = pojoToBeanHotel((Hotel) i.next());
            volBean.getHotels().add(hotelBean);
        }
        return volBean;
    }

    private Vol beanToPojoVol(VolBean volBean) {
        Vol vol = new Vol();
        vol.setIduser(volBean.getId());
        vol.setDate_A(volBean.getDate_A());
        vol.setDate_D(volBean.getDate_D());
        vol.setLieu_A(volBean.getLieu_A());
        vol.setLieu_D(volBean.getLieu_D());
        vol.setTarif(volBean.getTarif());

        return vol;
    }


    //HOTEL

    private HotelBean pojoToBeanHotel(Hotel hotel) {
        HotelBean hotelBean = new HotelBean();
        hotelBean.setIdhotel(hotel.getIdhotel());
        hotelBean.setNom(hotel.getNom());
        hotelBean.setAdresse(hotel.getAdresse());
        hotelBean.setStanding(hotel.getStanding());
        hotelBean.setTarif(hotel.getTarif());

        return hotelBean;
    }

    private Hotel beanToPojoHotel(HotelBean hotelBean) {
        Hotel hotel = new Hotel();
        hotel.setIdhotel(hotelBean.getIdhotel());
        hotel.setNom(hotelBean.getNom());
        hotel.setAdresse(hotelBean.getAdresse());
        hotel.setStanding(hotelBean.getStanding());
        hotel.setTarif(hotelBean.getTarif());

        return hotel;
    }

}